package com.lagou.homework121;

import java.util.Arrays;

/**
 * 第一阶段模块二第一题作业：
 *         定义一个长度为[16][16]的整型二维数组并输入或指定所有位置的元素值，
 *         分别实现二维数组中所有行和所有列中所有元素的累加和并打印。
 *         再分别实现二维数组中左上角到右下角和右上角到左下角所有元素的累加和并打印。
 */
public class TwoDimensionalArraysAccumulate {

    private final static int LENGTH = 16;

    int[][] arrays = new int[LENGTH][LENGTH];

    public TwoDimensionalArraysAccumulate() {
        initArray();
    }

    //初始化16*16数组
    private void initArray() {
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays.length; j++) {
                arrays[j][i] = i + 1;
            }
        }
    }

    //行累加
    private int getRowAccumulate(int[] array) {
        int sum = 0;
        for (int i = 1; i <= array.length; i++) {
            sum += i;
        }
        return sum;
    }

    //列累加
    private void getColumnAccumulate() {
        for (int i = 0; i < this.arrays.length; i++) {
            int sum = 0;
            for (int j = 0; j < this.arrays[i].length; j++) {
                sum += this.arrays[j][i];
            }
            if (i % 2 == 1)
                System.out.print("第" + (i + 1) + "列的和是：" + sum + "\t\t");
            else
                System.out.println("第" + (i + 1) + "列的和是：" + sum);
        }
    }

    //左上角到右下角累加
    private void getLt2RbAccumulate() {
        int sum = 0;
        // 左上角到右下角,行下标=列下标
        for (int i = 0; i < LENGTH; i++) {
            sum += arrays[i][i];
//            System.out.println("arrays[" + i + "][" + i + "] = " + arrays[i][i]);
        }
        System.out.println("左上角到右下角累加，结果是：" + sum);
    }

    //右上角到左下角所有元素的累加和
    private void getRt2LbAccumulate() {
        int sum = 0;
        //行下标
        for (int i = 0; i < LENGTH; i++) {
            //列下标
            for (int j = LENGTH - 1; j >= 0; j--) {
//                System.out.println("arrays[" + i + "][" + j + "] = " + arrays[i][j]);
                sum += arrays[i][j];
                i++;
            }
        }
        System.out.println("右上角到左下角累加，结果是：" + sum);
    }


    public void showArray() {
        for (int[] arr : arrays) {
            System.out.println(Arrays.toString(arr));
        }
    }

    public static void main(String[] args) {
        TwoDimensionalArraysAccumulate tdaa = new TwoDimensionalArraysAccumulate();

        tdaa.showArray();
        System.out.println("--------------------------------------------------------");
        System.out.println("由于初始化时各行数据都一样，所以各行的和都是：" + tdaa.getRowAccumulate(tdaa.arrays[0]));
//        System.out.println("各列的和是：" + tdaa.getRowAccumulate(tdaa.arrays[0]));
        System.out.println("--------------------------------------------------------");
        tdaa.getColumnAccumulate();
        System.out.println();
        System.out.println("--------------------------------------------------------");
        tdaa.getLt2RbAccumulate();
        System.out.println("--------------------------------------------------------");
        tdaa.getRt2LbAccumulate();

    }


}
